package dancrawler.server.gamecmd;

import dancrawler.proto.cmd.CmdHandler;
import dancrawler.server.Creature;
import dancrawler.server.GameHandler;
import dancrawler.server.GameObject;
import dancrawler.server.Tile;
import dancrawler.server.netMsg;
import java.util.List;

/**
 *
 */
public class CmdAttack extends CmdHandler {

    private GameHandler gh;

    public CmdAttack(GameHandler gh) {
        this.gh = gh;
    }

    @Override
    public void handleCmd(netMsg msg) {
        GameObject obj = gh.getPlayerCharacter(msg.getSender().getId());
        if (obj instanceof Creature) {
            Creature pcr = (Creature) obj;
            int angle = msg.getArgs()[0][0];
            angle += Byte.MAX_VALUE;
            Tile target = null;
            if (angle > 239 || angle < 16) {
                target = pcr.getWorld().getTile(pcr.getX() + 1, pcr.getY());
            } else if (angle > 208) {
                target = pcr.getWorld().getTile(pcr.getX() + 1, pcr.getY() - 1);
            } else if (angle > 176) {
                target = pcr.getWorld().getTile(pcr.getX(), pcr.getY() - 1);
            } else if (angle > 144) {
                target = pcr.getWorld().getTile(pcr.getX() - 1, pcr.getY() - 1);
            } else if (angle > 112) {
                target = pcr.getWorld().getTile(pcr.getX() - 1, pcr.getY());
            } else if (angle > 80) {
                target = pcr.getWorld().getTile(pcr.getX() - 1, pcr.getY() + 1);
            } else if (angle > 48) {
                target = pcr.getWorld().getTile(pcr.getX(), pcr.getY() + 1);
            } else if (angle > 16) {
                target = pcr.getWorld().getTile(pcr.getX() + 1, pcr.getY() + 1);
            }





            if (target == null) {
                return;
            }
            List<GameObject> gos = target.getContents();
            for (GameObject g : gos) {
                if (g instanceof Creature) {
                    Creature c = (Creature) g;
                    pcr.attack(c);
                }
            }
        }


    }

    @Override
    public int getId() {
        return 18;
    }

    @Override
    public CmdHandler getHandler() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
